package com.itheima.controller;

import com.itheima.doamin.ResultInfo;
import com.itheima.domain.Student;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/student")
    public ResultInfo findAll(){
        List<Student> studentList = studentService.findAll();
        return ResultInfo.success(studentList);
    }

    @GetMapping("/student/{number}")
    public ResultInfo findByNumber(@PathVariable String number){
        Student student = studentService.findByNumber(number);
        return ResultInfo.success(student);
    }

    @PostMapping("/student")
    public ResultInfo save(@RequestBody Student student){
        studentService.save(student);
        return ResultInfo.success(null);
    }

    @PutMapping("/student")
    public ResultInfo updateByNumber(@RequestBody Student student){
        studentService.updateByNumber(student);
        return ResultInfo.success(null);
    }

    @DeleteMapping("/student/{number}")
    public ResultInfo deleteByNumber(@PathVariable String number){
        studentService.deleteByNumber(number);
        return ResultInfo.success(null);
    }
}
